package com.elock.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elock.base.core.Filter;
import com.elock.base.core.Page;
import com.elock.base.core.Pageable;
import com.elock.base.core.controller.BaseController;
import com.elock.base.core.controller.ResultMsg;
import com.elock.base.core.util.JsonUtils;
import com.elock.entity.CompanyInfo;
import com.elock.service.CompanyInfoService;

/**
 * Controller - 企业信息
 *	@author 仁德
 */
@Controller("companyInfoController")
@RequestMapping("/company_info")
public class CompanyInfoController extends BaseController {

	@Resource(name = "companyInfoServiceImpl")
	private CompanyInfoService companyInfoService;

	/**
	 * 列表
	 * 
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Pageable pageable, ModelMap model) {

		CompanyInfo companyInfo = companyInfoService.getCurrent();
		model.addAttribute("companyInfo", companyInfo);

		return "/company_info/list";
	}

	@RequestMapping(value = "/list_tb", method = RequestMethod.GET)
	public String list_tb(ModelMap model) {

		Long companyInfoId = companyInfoService.getCurrentId();
		model.addAttribute("companyInfoId", companyInfoId);

		return "/company_info/list_tb";
	}

	/**
	 * 列表数据
	 */
	@RequestMapping(value = "/list_data", method = RequestMethod.POST)
	public @ResponseBody ResultMsg list_data(String company_name, String simple_name,
			String company_code, Pageable pageable) {

		CompanyInfo companyInfo = companyInfoService.getCurrent();
		List<Filter> filters = new ArrayList<Filter>();
		if (companyInfo != null) {
			filters.add(Filter.eq("id", companyInfo.getId()));
		}
		if (StringUtils.isNotEmpty(company_name)) {
			filters.add(Filter.like("company_name", "%" + company_name + "%"));
		}
		if (StringUtils.isNotEmpty(simple_name)) {
			filters.add(Filter.like("simple_name", "%" + simple_name + "%"));
		}
		if (StringUtils.isNotEmpty(company_code)) {
			filters.add(Filter.like("company_code", "%" + company_code + "%"));
		}
		Page<CompanyInfo> page = companyInfoService.findPage(filters, null, pageable);
		String jsonPage = JsonUtils.toJson(page);
		return ResultMsg.success(jsonPage);
	}

	/**
	 * 添加
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(ModelMap model) {

		return "/company_info/add";
	}

	/**
	 * 保存
	 * @param companyInfo
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody ResultMsg save(CompanyInfo companyInfo, Long mainCompanyId) {

		companyInfoService.saveCompanyInfo(companyInfo);
		return success();
	}

	/**
	 * 编辑
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(Long id, ModelMap model) {

		CompanyInfo company_info = companyInfoService.find(id);
		model.addAttribute("company_info", company_info);

		return "/company_info/edit";
	}

	/**
	 * 更新
	 * @param companyInfo
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody ResultMsg update(CompanyInfo companyInfo) {

		companyInfoService.updateCompanyInfo(companyInfo);

		return success();
	}

	/**
	 * 验证公司名称唯一性
	 * @param company_name
	 * @return
	 */
	@RequestMapping(value = "/check_compName", method = RequestMethod.GET)
	public @ResponseBody boolean check_name(String company_name, Long id) {
		if (id == null) {
			return !companyInfoService.exists(Filter.eq("company_name", company_name))
					&& !companyInfoService.exists(Filter.eq("simple_name", company_name));
		}
		else {
			return !companyInfoService.exists(Filter.eq("company_name", company_name),
					Filter.ne("id", id))
					&& !companyInfoService.exists(Filter.eq("simple_name", company_name),
							Filter.ne("id", id));
		}
	}

	/**
	 * 验证公司简称唯一性
	 * @param company_name
	 * @return
	 */
	@RequestMapping(value = "/check_simpleName", method = RequestMethod.GET)
	public @ResponseBody boolean check_simpleName(String simple_name, Long id) {
		if (id == null) {
			return !companyInfoService.exists(Filter.eq("company_name", simple_name))
					&& !companyInfoService.exists(Filter.eq("simple_name", simple_name));
		}
		else {
			return !companyInfoService.exists(Filter.eq("company_name", simple_name),
					Filter.ne("id", id))
					&& !companyInfoService.exists(Filter.eq("simple_name", simple_name),
							Filter.ne("id", id));
		}
	}

	/**
	 * 验证企业编码唯一性
	 * @param company_code
	 * @return
	 */
	@RequestMapping(value = "/check_compCode", method = RequestMethod.GET)
	public @ResponseBody boolean check_code(String company_code, Long id) {
		if (id == null) {
			return !companyInfoService.exists(Filter.eq("company_code", company_code));
		}
		else {
			return !companyInfoService.exists(Filter.eq("company_code", company_code),
					Filter.ne("id", id));
		}
	}

	/**
	 * 验证激活码唯一性
	 * @param company_code
	 * @return
	 */
	@RequestMapping(value = "/check_activCode", method = RequestMethod.GET)
	public @ResponseBody boolean check_activCode(String activateCode, Long id) {
		if (id == null) {
			return !companyInfoService.exists(Filter.eq("activateCode", activateCode));
		}
		else {
			return !companyInfoService.exists(Filter.eq("activateCode", activateCode),
					Filter.ne("id", id));
		}
	}

	/**
	 * 验证规则码长度
	 * @param pwd_code
	 * @return
	 */
	@RequestMapping(value = "/check_pwdCode", method = RequestMethod.GET)
	public @ResponseBody boolean check_pwdCode(String pwd_code) {
		if (pwd_code.length() == 5) {
			return true;
		}
		else {
			return false;
		}
	}

	/**
	 * 生成接口密钥
	 * @param pwd_code
	 * @return
	 */
	@RequestMapping(value = "/create_intf_key", method = RequestMethod.POST)
	public @ResponseBody ResultMsg createIntfKey(String company_code, String pwd_code) {
		return success().addObjX(DigestUtils.md5Hex(company_code + pwd_code));
	}

}
